home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.1 / Libraries / GadTools / menu1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  8.5 KB  |  397 lines

  1. /*
  2. ** menu1.c:  Menu example with font-sensitivity.
  3. **
  4. ** (C) Copyright 1990, Commodore-Amiga, Inc.
  5. **
  6. ** Executables based on this information may be used in software
  7. ** for Commodore Amiga computers.  All other rights reserved.
  8. ** This information is provided "as is"; no warranties are made.  All
  9. ** use is at your own risk. No liability or responsibility is assumed.
  10. */
  11.  
  12. #include <exec/types.h>
  13. #include <intuition/intuition.h>
  14. #include <intuition/gadgetclass.h>
  15. #include <libraries/gadtools.h>
  16.  
  17. #include <clib/exec_protos.h>
  18. #include <clib/graphics_protos.h>
  19. #include <clib/intuition_protos.h>
  20. #include <clib/diskfont_protos.h>
  21. #include <clib/gadtools_protos.h>
  22.  
  23. void printf(STRPTR,...);
  24. int stcd_l(char *, long *);
  25. void exit(int);
  26.  
  27. /*------------------------------------------------------------------------*/
  28.  
  29. void main(int, char *[]);
  30. void bail_out(int);
  31. BOOL HandleMenuEvent(UWORD);
  32. BOOL OpenFunc(UWORD);
  33. BOOL SaveFunc(UWORD);
  34. BOOL PrintFunc(UWORD);
  35. BOOL QuitFunc(UWORD);
  36.  
  37. /*------------------------------------------------------------------------*/
  38.  
  39. /* Here we specify what we want our menus to contain: */
  40.  
  41. struct NewMenu mynewmenu[] =
  42. {
  43.     { NM_TITLE, "Project",      0 , 0, 0, 0,},
  44.     {  NM_ITEM, "Open...",     "O", 0, 0, OpenFunc,},
  45.     {  NM_ITEM, "Save",      0 , 0, 0, SaveFunc,},
  46.     {  NM_ITEM, NM_BARLABEL,  0 , 0, 0, 0,},
  47.     {  NM_ITEM, "Print",      0 , 0, 0, 0,},
  48.     {   NM_SUB, "Draft",      0 , 0, 0, PrintFunc,},
  49.     {   NM_SUB, "NLQ",      0 , 0, 0, PrintFunc,},
  50.     {  NM_ITEM, NM_BARLABEL,  0 , 0, 0, 0,},
  51.     {  NM_ITEM, "Quit...",     "Q", 0, 0, QuitFunc,},
  52.  
  53.     { NM_TITLE, "Edit",      0 , 0, 0, 0,},
  54.     {  NM_ITEM, "Cut",     "X", 0, 0, 0,},
  55.     {  NM_ITEM, "Copy",     "C", 0, 0, 0,},
  56.     {  NM_ITEM, "Paste",     "V", 0, 0, 0,},
  57.     {  NM_ITEM, NM_BARLABEL,  0 , 0, 0, 0,},
  58.     {  NM_ITEM, "Undo",     "Z", 0, 0, 0,},
  59.  
  60.     {   NM_END, 0,          0 , 0, 0, 0,},
  61. };
  62.  
  63. /*------------------------------------------------------------------------*/
  64.  
  65. struct TextAttr customtattr;
  66. struct TextAttr *tattr;
  67.  
  68. /*------------------------------------------------------------------------*/
  69.  
  70. extern struct Library *SysBase;
  71. struct GfxBase *GfxBase = NULL;
  72. struct IntuitionBase *IntuitionBase = NULL;
  73. struct Library *GadToolsBase = NULL;
  74. struct Library *DiskfontBase = NULL;
  75. struct Screen *mysc = NULL;
  76. struct Menu *menu = NULL;
  77. struct Window *mywin = NULL;
  78. struct TextFont *customfont = NULL;
  79. void *vi = NULL;
  80.  
  81. /*------------------------------------------------------------------------*/
  82.  
  83. BOOL terminated;
  84.  
  85. /*------------------------------------------------------------------------*/
  86.  
  87. void main(argc, argv)
  88.  
  89. int argc;
  90. char *argv[];
  91.  
  92. {
  93.     struct IntuiMessage *imsg;
  94.     ULONG imsgClass;
  95.     UWORD imsgCode;
  96.     ULONG errorcode = 0;
  97.  
  98.     terminated = FALSE;
  99.  
  100.     if (argc == 2)
  101.     {
  102.     printf("Usage:\n\tmenu1\nor\n\tmenu1 fontname.font fontsize\n");
  103.     printf("Example:\n\tmenu1 courier.font 15\n");
  104.     bail_out(0);
  105.     }
  106.     /* Open all libraries: */
  107.  
  108.     if (!(GfxBase = (struct GfxBase *)
  109.     OpenLibrary("graphics.library", 36L)))
  110.     bail_out(20);
  111.  
  112.     if (!(IntuitionBase = (struct IntuitionBase *)
  113.     OpenLibrary("intuition.library", 36L)))
  114.     bail_out(20);
  115.  
  116.     if (!(GadToolsBase = OpenLibrary("gadtools.library", 36L)))
  117.     bail_out(20);
  118.  
  119.     if (!(DiskfontBase = OpenLibrary("diskfont.library", 36L)))
  120.     bail_out(20);
  121.  
  122.     if (!(mysc = LockPubScreen(NULL)))
  123.     bail_out(20);
  124.  
  125.     if (!(vi = GetVisualInfo(mysc,
  126.     TAG_DONE)))
  127.     bail_out(20);
  128.  
  129.     customtattr.ta_Style = 0;
  130.     customtattr.ta_Flags = 0;
  131.     if (argc < 3)
  132.     {
  133.     /* Default to screen's font */
  134.     tattr = mysc->Font;
  135.     }
  136.     else
  137.     {
  138.     LONG longval;
  139.  
  140.     /* Attempt to use the font specified on the command line: */
  141.     customtattr.ta_Name = argv[1];
  142.     /* Convert decimal size to long */
  143.     stcd_l(argv[2], &longval);
  144.     customtattr.ta_YSize = longval;
  145.     tattr = &customtattr;
  146.     if (!(customfont = OpenDiskFont(tattr)))
  147.     {
  148.         printf("Could not open font %s %ld\n", customtattr.ta_Name,
  149.         customtattr.ta_YSize);
  150.         bail_out(20);
  151.     }
  152.     }
  153.  
  154.     /* Build and layout menus using the right font: */
  155.     if (!(menu = CreateMenus(mynewmenu,
  156.     GTMN_FrontPen, 0,
  157.     GTMN_SecondaryError, &errorcode,
  158.     TAG_DONE)))
  159.     {
  160.     switch ( errorcode )
  161.     {
  162.         case GTMENU_INVALID:
  163.         /* Bad ordering of menu elements in the NewMenu structure. */
  164.         printf("Invalid menu structure!\n");
  165.         break;
  166.  
  167.         case GTMENU_NOMEM:
  168.         printf("Out of memory in CreateMenus()!\n");
  169.         break;
  170.  
  171.         default:
  172.         printf("Failed to create menus for unknown reason\n");
  173.         break;
  174.     }
  175.     bail_out(20);
  176.     }
  177.  
  178.     if (errorcode == GTMENU_TRIMMED)
  179.     {
  180.     /* This condition does not cause failure.  Your menu will
  181.      * be trimmed instead.  (This won't happen in the example, since
  182.      * the maximum number of menus, items, or sub-items is not
  183.      * exceeded.  The test is included to demonstrate its use.
  184.      */
  185.     printf(" Too many menu elements -- some have been trimmed off\n");
  186.     }
  187.  
  188.     if (!LayoutMenus(menu, vi,
  189.     GTMN_TextAttr, tattr,
  190.     TAG_DONE))
  191.     bail_out(20);
  192.  
  193.     if (!(mywin = OpenWindowTags(NULL,
  194.     WA_Width, 500,
  195.     WA_InnerHeight, 100,
  196.  
  197.     WA_Activate, TRUE,
  198.     WA_DragBar, TRUE,
  199.     WA_DepthGadget, TRUE,
  200.     WA_CloseGadget, TRUE,
  201.     WA_SizeGadget, TRUE,
  202.     WA_SimpleRefresh, TRUE,
  203.  
  204.     /* NOTE: NOCAREREFRESH is not allowed if you use GadTools Gadgets! */
  205.     WA_NoCareRefresh, TRUE,
  206.  
  207.     WA_IDCMP, CLOSEWINDOW | MENUPICK,
  208.  
  209.     WA_MinWidth, 50,
  210.     WA_MinHeight, 50,
  211.     WA_Title, "GadTools Menu Demo",
  212.  
  213.     TAG_DONE)))
  214.     bail_out(20);
  215.  
  216.     SetMenuStrip(mywin, menu);
  217.  
  218.     while (!terminated)
  219.     {
  220.     Wait (1 << mywin->UserPort->mp_SigBit);
  221.     /* NOTE:  If you use GadTools gadgets, you must use GT_GetIMsg()
  222.      * and GT_ReplyIMsg() instead of GetMsg() and ReplyMsg().
  223.      */
  224.     while ((!terminated) && (imsg = (struct IntuiMessage *)GetMsg(mywin->UserPort)))
  225.     {
  226.         imsgClass = imsg->Class;
  227.         imsgCode = imsg->Code;
  228.         ReplyMsg(imsg);
  229.         switch (imsgClass)
  230.         {
  231.         case MENUPICK:
  232.             terminated = HandleMenuEvent(imsgCode);
  233.             break;
  234.  
  235.         case CLOSEWINDOW:
  236.             printf("CLOSEWINDOW.\n");
  237.             terminated = TRUE;
  238.             break;
  239.         }
  240.     }
  241.     }
  242.     bail_out(0);
  243. }
  244.  
  245. /*------------------------------------------------------------------------*/
  246.  
  247. /*/ bail_out()
  248.  *
  249.  * Function to close down or free any opened or allocated stuff, and then
  250.  * exit.
  251.  *
  252.  */
  253.  
  254. void bail_out(code)
  255.  
  256. int code;
  257.  
  258. {
  259.     if (mywin)
  260.     {
  261.     ClearMenuStrip(mywin);
  262.     CloseWindow(mywin);
  263.     }
  264.  
  265.     /* None of these two calls mind a NULL parameter, so it's not
  266.      * necessary to check for non-NULL before calling.  If we do that,
  267.      * we must be certain that the OpenLibrary() of GadTools succeeded,
  268.      * or else we would be jumping into outer space:
  269.      */
  270.     if (GadToolsBase)
  271.     {
  272.     FreeMenus(menu);
  273.     FreeVisualInfo(vi);
  274.     CloseLibrary(GadToolsBase);
  275.     }
  276.  
  277.     if (customfont)
  278.     {
  279.     CloseFont(customfont);
  280.     }
  281.  
  282.     if (mysc)
  283.     {
  284.     UnlockPubScreen(NULL, mysc);
  285.     }
  286.  
  287.     if (DiskfontBase)
  288.     {
  289.     CloseLibrary(DiskfontBase);
  290.     }
  291.  
  292.     if (IntuitionBase)
  293.     {
  294.     CloseLibrary(IntuitionBase);
  295.     }
  296.  
  297.     if (GfxBase)
  298.     {
  299.     CloseLibrary(GfxBase);
  300.     }
  301.  
  302.     exit(code);
  303. }
  304.  
  305.  
  306. /*------------------------------------------------------------------------*/
  307.  
  308. /*/ HandleMenuEvent()
  309.  *
  310.  * This function handles IntuiMessage events of type MENUPICK.  It
  311.  * demonstrates one of the best uses for the MenuItem UserData field
  312.  * provided by GadTools, namely a place to store pointers-to-functions.
  313.  *
  314.  */
  315.  
  316. BOOL HandleMenuEvent(code)
  317.  
  318. UWORD code;
  319.  
  320. {
  321.     struct MenuItem *item;
  322.     BOOL terminated = FALSE;
  323.     BOOL (*fptr)(UWORD);
  324.  
  325.     printf("MENUPICK:  ");
  326.     /* Get all menu events including NextEvents until a terminating
  327.      * selection is made (such as Quit)
  328.      */
  329.     while ((code != MENUNULL) && (!terminated))
  330.     {
  331.     item = ItemAddress(menu, code);
  332.     if (fptr = GTMENUITEM_USERDATA(item))
  333.     {
  334.         terminated = (*fptr)(code);
  335.     }
  336.     else
  337.     {
  338.         printf("No function.  ");
  339.     }
  340.     code = item->NextSelect;
  341.     }
  342.     printf("\n");
  343.  
  344.     return(terminated);
  345. }
  346.  
  347. /*------------------------------------------------------------------------*/
  348.  
  349. BOOL OpenFunc(code)
  350.  
  351. UWORD code;
  352.  
  353. {
  354.     printf("OpenFunc called.  ");
  355.     return(FALSE);
  356. }
  357.  
  358. /*------------------------------------------------------------------------*/
  359.  
  360. BOOL SaveFunc(code)
  361.  
  362. UWORD code;
  363.  
  364. {
  365.     printf("SaveFunc called.  ");
  366.     return(FALSE);
  367. }
  368. /*------------------------------------------------------------------------*/
  369.  
  370. BOOL PrintFunc(code)
  371.  
  372. UWORD code;
  373.  
  374. {
  375.     printf("PrintFunc called ");
  376.     if (code == FULLMENUNUM(0, 3, 0))
  377.     {
  378.     printf("(Draft).  ");
  379.     }
  380.     else
  381.     {
  382.     printf("(NLQ).  ");
  383.     }
  384.     return(FALSE);
  385. }
  386. /*------------------------------------------------------------------------*/
  387.  
  388. BOOL QuitFunc(code)
  389.  
  390. UWORD code;
  391.  
  392. {
  393.     printf("QuitFunc called.  ");
  394.     return(TRUE);
  395. }
  396. /*------------------------------------------------------------------------*/
  397.